// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import 'babel-polyfill';
// import Vue from "vue";
import App from './App';
Vue.prototype.vue = Vue;

import router from './router';

router.beforeEach((to, from, next) => {
  const token = Vue.prototype.func.getToken();
  if (token) {
    next();
  } else {
    // 没有登录，去跳转登录页
    if (to.path === '/setting/login') {
      next();
    } else {
      // 没有登录，去跳转登录页
      // location.href = "/#/setting/login";
      next({
        path:"/setting/login"
      });
    }
  }
})

router.onError(error => {
  const pattern = /Loading chunk (\d)+ failed/g;
  const isChunkLoadFailed = error.message.match(pattern);
  const targetPath = router.history.pending.fullPath;
  if (isChunkLoadFailed) {
    router.replace(targetPath);
  }
})

Vue.prototype.$ELEMENT = { size: 'small' }

import addComponents from "./components/shared/high-components";
Vue.use(addComponents);

import VueAnimateNumber from 'vue-animate-number'
Vue.use(VueAnimateNumber);

import preview from 'vue-photo-preview'
import 'vue-photo-preview/dist/skin.css'
let options = {
  fullscreenEl: false
};
Vue.use(preview, options)

//封装的axios
import axios from "@/server/index.js";
Vue.prototype.$axios = axios.axios;
import server from '@/server/https.js';
Vue.prototype.https = server.https;
Vue.prototype.httpUpload = server.httpUpload;
Vue.prototype.configs = axios.configs;


import VueAwesomeSwiper from 'vue-awesome-swiper'
Vue.use(VueAwesomeSwiper, /* { 全局组件的默认选项 } */)

//自定义指令
import "@/utils/directive.js";
import "@/utils/filter.js";
import func from "@/utils/func.js";
func.arrModelkeyValues = function (arr) {
  let obj = new Object();
  arr.map(item => {
    obj[item.modelKey] = item.modelValues || null;
  })
  return obj;
}
Vue.prototype.func = func;
Vue.prototype.$bus = new Vue();

/*引入公共样式*/
import 'element-ui/lib/theme-chalk/index.css';
import "../../assets/font/iconfont.css"
import "../../assets/css/style.css";
import "./assets/css/style.css";

new Vue({
  el: '#app',
  router,
  data() {
    return {
      tenantInfo: {},
      tenantCode: "test",
      query: {},
    }
  },
  created() {
    this.query = parseQueryString("hash");
  },
  mounted() {
    this.tenantInfo = this.func.getSession("tenantInfo") || {};
    this.tenantCode = this.func.getSession("tenantCode") || this.tenantInfo.code || "1";
    // this.getTenant();
    // if (this.configs.NODE_ENV != "dev") {
    //   document.oncontextmenu = function () {
    //     return false;
    //   };
    //   document.onkeydown = function (event) {
    //     if (event.which == 123) {
    //       return false;
    //     }
    //   }
    // }
  },
  methods: {
    getTenant() {
      let params = {
        tenantCode: this.tenantCode,
      }
      this.https("get", `/api/Tenant/GettenantConfig`, params).then(res => {
        let data = res.response || {};
        data.name = data.tenantName;
        this.tenantInfo = Object.assign(this.tenantInfo, data);
        this.func.setSession("tenantInfo", this.tenantInfo);
      })
    },
    back(path, query = {}) {
      if (path) {
        this.$router.replace(path, query);
      } else {
        if (window.history.length <= 1 || path === 0) {
          window.opener = null;
          window.close();
        } else {
          this.$router.go(-1);
        }
      }
    }
  },
  render: h => h(App)
})
